#coding=utf-8
'''
Created on 9.30, 2016
@author: wangpeng
'''
import unittest
from appium import webdriver
from selenium.webdriver.common.by import By   #用于匹配某个值是否出现，智能等待
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
import time
import common.config
import common.logging_save_huawei_p8
import common.desired_setting_huawei_p8
import threading
import re
import common.Create_Log_Folder
import os
import os.path
import common.logging_save_huawei_p8
import serial
#import common.save_serial_log
import sys
reload(sys)
sys.setdefaultencoding( "utf-8" )
import subprocess

'''
def test():
    desired_setting = common.desired_setting_huawei_p8.desired_setting()
    driver = webdriver.Remote('http://localhost:4723/wd/hub', desired_setting)
    common.logging_save_huawei_p8.logging.info("返回到Home主菜单界面")
    print time.ctime(),"返回到Home主菜单界面"
    driver.keyevent(3)  #返回到主菜单 
    common.logging_save_huawei_p8.logging.info("升级测试开始")   
    print time.ctime(),"升级测试开始"
    
    time.sleep(3)
    common.logging_save_huawei_p8.logging.info("进入小雷达APP界面")
    driver.tap([(955,514)], 1)  #点击小雷达APP界面 
    time.sleep(20) 
    
    for testimes in range(5000):
        #driver = webdriver.Remote('http://localhost:4723/wd/hub', desired_setting) 
        common.logging_save_huawei_p8.logging.info("当前升级测试次数为第 "+str(testimes+1)+" 次")
                 
        #os.system(r'D:\power\power.exe com4 pw_on 7')   #然后AC上电
        #common.logging_save_huawei_p8.logging.info("AC上电，等待3秒")
        #time.sleep(3)          
        time.sleep(10)
        common.logging_save_huawei_p8.logging.info("点击SETTING")        
        driver.tap([(82,163)], 1)  #点击SETTING  
        time.sleep(5)
        common.logging_save_huawei_p8.logging.info("点击关于")        
        driver.tap([(320,1169)], 1)  #点击关于 
        time.sleep(5)        
        common.logging_save_huawei_p8.logging.info("点击更新")
        driver.tap([(475,911)], 1)  #点击更新
        
        time.sleep(5)        
        common.logging_save_huawei_p8.logging.info("点击BLE版本")
        driver.tap([(556,485)], 1)  #点击BLE版本
        
        time.sleep(2)        
        common.logging_save_huawei_p8.logging.info("点击升级")
        driver.tap([(765,1095)], 1)  #点击升级 
        
        time.sleep(180)

test()         
'''
        
class Test(unittest.TestCase):
    def setUp(self):
        os.system('taskkill /f /im adb.exe')
        common.logging_save_huawei_p8.logging.info("app测试开始，正在开始启动app")
        print time.ctime(),"app测试开始，正在开始启动app"
        self.begin_time_for_serial = time.strftime("%y%m%d_%H:%M:%S", time.localtime()) 
        self.save_log = common.Create_Log_Folder.Create("\\huawei_p8\\upgrade_cancel_speical")    #建立report保存路径
        common.logging_save_huawei_p8.logging.info("反复升级测试，升级过程中判断是否死机")
        print time.ctime(),"反复升级测试，观察是否升级是否会出现升级卡主不动的现象，或者机器死机"
        sys.path.append('\adbcommand')  #增加adb log
        self.test = subprocess.Popen(r'E:\eclipse\huawei_rader\all_mobile\testcase\huawei_p8\adbcommond\upgrade_cancel_speical.bat') #导入adb
        time.sleep(1)
        #开启appium设置
        self.desired_setting = common.desired_setting_huawei_p8.desired_setting()
        self.driver = webdriver.Remote('http://localhost:4723/wd/hub', self.desired_setting)

        time.sleep(5)
        #由于V0.2.3版本突然有一个Bug，一开打app就会弹出来一个更新遥控器，所以临时添加确认下

        common.logging_save_huawei_p8.logging.info("app连接BLE中，70秒后再做判定。。。")
        print time.ctime(),"app连接BLE中，70秒再做判定。。。"
        time.sleep(30)
        
        try:
            self.driver.find_element_by_id("com.huawei.smallRadar:id/cancel").click()
            time.sleep(3)
            common.logging_save_huawei_p8.logging.info("close弹出框")
            print time.ctime(),"close弹出框"

        except:
            pass
        
    def tearDown(self):
        #self.driver.quit()
        time.sleep(1)
        self.test.kill()
        os.system('taskkill /f /im adb.exe')
        print '关闭adb log'
        time.sleep(10)
        print 'finished'
      
    def test_play(self):
        u"""测试步骤：反复进行升级测试,升级10秒钟左右，然后退出升级， 测试200次"""
        savenumber = 1
        save_pic = os.path.join(self.save_log+str(savenumber)+'.png')
        #判断是否进入主界面
        try:
            WebDriverWait(self.driver, 5).until(EC.presence_of_element_located((By.ID,common.config.wendu()))) #确认是否进入到主界面                       
            common.logging_save_huawei_p8.logging.info("经过确认，已经进入到主菜单页面")
            print time.ctime(),"经过确认，已经进入到主菜单页面"
            time.sleep(3)
        except:
            self.driver.get_screenshot_as_file(save_pic)

        self.driver.find_element_by_class_name(common.config.menu_setting_click()).click()        
        common.logging_save_huawei_p8.logging.info("进入到setting设置界面")
        print time.ctime(),"进入到setting设置界面"     
        time.sleep(3)
        self.driver.find_element_by_xpath(common.config.setting_menu_guanyu()).click()
        time.sleep(3)        
        self.driver.find_element_by_xpath(common.config.click_menu_updage_xpath()).click()
        time.sleep(5)
        testtime = 1

        while(testtime <=2000):
            try:
                self.driver.find_element_by_xpath(common.config.click_BLE_update_xpath()).click()
                time.sleep(5)
                
                try:
                    self.driver.find_element_by_id("com.huawei.smallRadar:id/submit").click()
                    time.sleep(3)
                    common.logging_save_huawei_p8.logging.info("点击升级操作")
                    print time.ctime(),"点击升级操作"
        
                except:
                    pass
    
                common.logging_save_huawei_p8.logging.info("过15秒钟之后就按退出按键")
                print time.ctime(),"过15秒钟之后就按退出按键"
                time.sleep(15)
                
                self.driver.keyevent(4)
                common.logging_save_huawei_p8.logging.info("按返回按键")
                print time.ctime(),"按返回按键"
                time.sleep(30)
                def valiate_ok():
                    try:
                        WebDriverWait(self.driver, 3).until(EC.presence_of_element_located((By.ID,common.config.BLE_disconnect())))
                        common.logging_save_huawei_p8.logging.info("发现蓝牙断开，发生在第："+str(testtime)+"次")
                        print time.ctime(),"发现蓝牙断开，发生在第："+str(testtime)+"次"
                        a = 1
                        self.driver.get_screenshot_as_file(save_pic)
                        return a
                                                    
                    except:
                        common.logging_save_huawei_p8.logging.info("经过确认，进入到app连接成功次数为第："+str(testtime)+"次")
                        print time.ctime(),"经过确认，进入到app连接成功次数为第："+str(testtime)+"次"
                        a= 0
                        return a
                
                self.assertNotEqual(valiate_ok(), 1, "发现连接断开现象，请确认问题所在")
                common.logging_save_huawei_p8.logging.info("经过确认，蓝牙正常连接成功")
                print time.ctime(),"经过确认，蓝牙正常连接成功"
                common.logging_save_huawei_p8.logging.info("当前测试次数为第"+str(testtime)+"次")
                print time.ctime(),"当前测试次数为第"+str(testtime)+"次"
                testtime = testtime + 1
            except:            
                self.driver.quit()
                time.sleep(5)
                self.driver = webdriver.Remote('http://localhost:4723/wd/hub', self.desired_setting)
                common.logging_save_huawei_p8.logging.info("重新关闭app，再打开app")
                print time.ctime(),"重新关闭app，再打开app"  
                time.sleep(40)
                try:
                    self.driver.find_element_by_id("com.huawei.smallRadar:id/cancel").click()
                    time.sleep(3)
                    common.logging_save_huawei_p8.logging.info("close弹出框")
                    print time.ctime(),"close弹出框"
        
                except:
                    pass     
                  
                try:
                    WebDriverWait(self.driver, 5).until(EC.presence_of_element_located((By.ID,common.config.wendu()))) #确认是否进入到主界面                       
                    common.logging_save_huawei_p8.logging.info("经过确认，已经进入到主菜单页面")
                    print time.ctime(),"经过确认，已经进入到主菜单页面"
                    time.sleep(3)
                except:
                    self.driver.get_screenshot_as_file(save_pic)
        
                self.driver.find_element_by_class_name(common.config.menu_setting_click()).click()        
                common.logging_save_huawei_p8.logging.info("进入到setting设置界面")
                print time.ctime(),"进入到setting设置界面"     
                time.sleep(3)
                self.driver.find_element_by_xpath(common.config.setting_menu_guanyu()).click()
                time.sleep(3)        
                self.driver.find_element_by_xpath(common.config.click_menu_updage_xpath()).click()
                time.sleep(5)
                testtime = 1              
                
if __name__ == "__main__":
    unittest.main()
 